package com.softhinker.ke24.unit2.settings;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.softhinker.ke24.unit2.MainFace.SettingsPanelCreator;
import com.softhinker.ke24.unit2.utils.SpringUtilities;

public class SettingsPanel implements SettingsPanelCreator, ActionListener {

	private static final String TIMESTRING = "By time";
	private static final String ITERATIONSTRING = "By iteration";
	
	private static final int DEFAULT_MUTATION_RATE = 50;
	//private static final double DEFAULT_CROSSOVER_RATE = 0.5;
	private static final int DEFAULT_POPULATION_SIZE = 1000;
	private static final int DEFAULT_ITERATION_DURATION = 3;//in minute
	private static final int DEFAULT_ITERATION_COUNT = 1000;
    public static final String FIXED_STRING = "Fixed";
    public static final String DECREASING_STRING = "Decreasing";
	
	private JTextField timeField = new JTextField(6);
	private JTextField iterationField = new JTextField(6);
	private JTextField mutationRateField = new JTextField(10);
	//private JTextField crossoverRateField = new JTextField(10);
	private JTextField populationSizeField = new JTextField(10);
	
	private JRadioButton decreasingOption;
	private JRadioButton fixedOption;
	private ButtonGroup adaptStrategy;
	
	public JComponent createSettingsPanel() {
		JPanel panel = new JPanel();
		
		/********** Tunning Parameter *************/
		JPanel paramPanel = new JPanel();
		paramPanel.setBorder(BorderFactory.createTitledBorder("Tuning Parameter"));
		SpringLayout layout = new SpringLayout();
		paramPanel.setLayout(layout);
		
		JLabel mutationRateLabel = new JLabel("Mutation Rate : 1 /");
		paramPanel.add(mutationRateLabel);
		mutationRateField.setText(String.valueOf(DEFAULT_MUTATION_RATE));//Default value
		mutationRateLabel.setLabelFor(mutationRateField);
		paramPanel.add(mutationRateField);
		
		//JLabel crossoverRateLabel = new JLabel("Crossover Rate : ");
		//paramPanel.add(crossoverRateLabel);
		//crossoverRateField.setText(String.valueOf(DEFAULT_CROSSOVER_RATE));
		//paramPanel.add(crossoverRateField);
		JLabel adaptStrategyLbl = new JLabel("Mutation rate is: ");
		paramPanel.add(adaptStrategyLbl);
		
		JPanel strategyPanel = new JPanel();
		
		
		fixedOption = new JRadioButton(FIXED_STRING);
		fixedOption.setActionCommand(FIXED_STRING);
		fixedOption.setSelected(true);
		strategyPanel.add(fixedOption);
		
		decreasingOption = new JRadioButton(DECREASING_STRING);
		decreasingOption.setActionCommand(DECREASING_STRING);
		decreasingOption.setSelected(true);
		strategyPanel.add(decreasingOption );
		
		adaptStrategy = new ButtonGroup();
		adaptStrategy.add(fixedOption);
		adaptStrategy.add(decreasingOption);

		paramPanel.add(strategyPanel);
		
		JLabel populationSizeLabel = new JLabel("Population Size : ");
		paramPanel.add(populationSizeLabel);
		populationSizeField.setText(String.valueOf(DEFAULT_POPULATION_SIZE));
		populationSizeLabel.setLabelFor(populationSizeField);
		paramPanel.add(populationSizeField);
		
		//Lay out the panel.
		SpringUtilities.makeCompactGrid(paramPanel,
		                                3, 2, //rows, cols
		                                3, 3,        //initX, initY
		                                3, 3);       //xPad, yPad
		
		panel.add(paramPanel);
		
		/********** Stopping Condition *************/
		JPanel stoppingConditionPanel = new JPanel();
		stoppingConditionPanel.setBorder(BorderFactory.createTitledBorder("Stopping Condition"));
		stoppingConditionPanel.setLayout(new SpringLayout());
		
		JRadioButton timeButton = new JRadioButton(TIMESTRING);
		timeButton.setActionCommand(TIMESTRING);
		timeButton.addActionListener(this);
		timeButton.setSelected(false);
		stoppingConditionPanel.add(timeButton);
		timeField.setEnabled(false);
		JLabel timeLabel = new JLabel("minute(s)");
		timeLabel.setLabelFor(timeField);
		stoppingConditionPanel.add(timeField);
		stoppingConditionPanel.add(timeLabel);
		
		JRadioButton iterationButton = new JRadioButton(ITERATIONSTRING);
		iterationButton.setActionCommand(ITERATIONSTRING);
		iterationButton.setSelected(true);
		iterationButton.addActionListener(this);
		stoppingConditionPanel.add(iterationButton);
		iterationField.setEnabled(true);
		iterationField.setText(String.valueOf(DEFAULT_ITERATION_COUNT));
		JLabel iterationLabel = new JLabel("iteration(s)");
		stoppingConditionPanel.add(iterationField);
		stoppingConditionPanel.add(iterationLabel);
		
		ButtonGroup group = new ButtonGroup();
	    group.add(timeButton);
	    group.add(iterationButton);
		
	    SpringUtilities.makeCompactGrid(stoppingConditionPanel,
                2, 3, //rows, cols
                3, 3,        //initX, initY
                3, 3);       //xPad, yPad
	    
	    panel.add(stoppingConditionPanel);
	    
		return panel;
	}

	public void actionPerformed(ActionEvent event) {
		if(TIMESTRING.equals(event.getActionCommand()))
		{
			timeField.setEnabled(true);
			timeField.setText(String.valueOf(DEFAULT_ITERATION_DURATION));
			iterationField.setEnabled(false);
			iterationField.setText(null);
		}
		else if(ITERATIONSTRING.equals(event.getActionCommand()))
		{
			iterationField.setEnabled(true);
			iterationField.setText(String.valueOf(DEFAULT_ITERATION_COUNT));
			timeField.setEnabled(false);
			timeField.setText(null);
		}
	}

	public int getMutationRate()
	{
		String value = this.mutationRateField.getText();
		int rate = 0;
		
		if(value == null || "".equals(value))
			return rate;
		
		try
		{
			rate = Integer.parseInt(value);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return DEFAULT_MUTATION_RATE;
		}
		return rate;
	}
	
	/*
	public double getCrossoverRate()
	{
		String value = this.crossoverRateField.getText();
		double rate = 0;
		
		if(value == null || "".equals(value))
			return rate;
		
		try
		{
			rate = Double.parseDouble(value);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return DEFAULT_CROSSOVER_RATE;
		}
		return rate;
	}
	*/
	
	public int getPopulationSize()
	{
		String value = this.populationSizeField.getText();
		int size = 0;
		
		if(value == null || "".equals(value))
			return size;
		
		try
		{
			size = Integer.parseInt(value);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return DEFAULT_POPULATION_SIZE;
		}
		return size;
	}
	
	public int getIterationCount()
	{
		String value = this.iterationField.getText();
		int count = 0;
		
		if(value == null || "".equals(value))
			return count;
		
		try
		{
			count = Integer.parseInt(value);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return DEFAULT_ITERATION_COUNT;
		}
		return count;
	}
	
	public int getIterationDuration()
	{
		String value = this.timeField.getText();
		int duration = 0;
		
		if(value == null || "".equals(value))
			return duration;
		
		try
		{
			duration = Integer.parseInt(value);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return DEFAULT_ITERATION_DURATION;
		}
		return duration;
	}
	
	public String getMutationRateStrategy() {
	    String selected = this.adaptStrategy.getSelection().getActionCommand();
	    System.out.println(selected);
	    return selected;
	}
}
